package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.drg.center.entity.DictProjectPO;
import com.drg.center.mapper.DictProjectMapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DictProjectDAO extends BaseDAO<DictProjectMapper, DictProjectPO> {

    public List<DictProjectPO> searchProject(String search, Boolean isMain, Integer projectType) {
        LambdaQueryWrapper<DictProjectPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictProjectPO::getIsMain, isMain);
        queryWrapper.eq(DictProjectPO::getProjectType, projectType);
        if (StringUtils.isNotBlank(search)) {
            queryWrapper.and(wrapper -> wrapper.like(DictProjectPO::getProjectCode, search).or().like(DictProjectPO::getProjectName, search));
        }
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }

    public int setMainProject(List<Long> ids) {
        LambdaUpdateWrapper<DictProjectPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DictProjectPO::getIsMain, Boolean.TRUE);
        updateWrapper.in(DictProjectPO::getId, ids);
        return getBaseMapper().update(new DictProjectPO(), updateWrapper);
    }

    public int cancelMainProject(List<Long> ids) {
        LambdaUpdateWrapper<DictProjectPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DictProjectPO::getIsMain, Boolean.FALSE);
        updateWrapper.in(DictProjectPO::getId, ids);
        return getBaseMapper().update(new DictProjectPO(), updateWrapper);
    }
}
